//
//  NDAuthenBaseView.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/17.
//

import SwiftUI

struct NDAuthenBaseView<Content: View>: View {

    let content: Content
    var baseConfig: NDBaseConfig
    var navigationConfig: NavigationConfig
    
    @Environment(\.presentationMode) var presentationMode
    @State private var showCustomPopup: Bool = false
    @Environment(\.navigationManager) var navManager
    
    init(baseConfig: NDBaseConfig = NDBaseConfig(),
         navigationConfig: NavigationConfig = NavigationConfig(title: ""),
         @ViewBuilder content: () -> Content) {
        self.baseConfig = baseConfig
        self.navigationConfig = navigationConfig
        self.content = content()
    }
    
    var body: some View {
        
        let buttons: [NDPopupButton] = [
            NDPopupButton.init(localizedString("Finished soon"), style: .default) {
                showCustomPopup = false
            },
            NDPopupButton.init(localizedString("Give up the quota"), style: .underline) {
                showCustomPopup = false
                navManager.wrappedValue.popToView(tag: mainNav, viewType: NDProductDetailView.self, animated: true)
            }
        ]

        NDBaseContentView(navigationConfig: navigationConfig) {
            ZStack {
                content
            }
            .toolbar {
                NavigationToolbarContent(
                    showBackButton: true,
                    backButtonColor: Color(hex: "#000000"),
                    image: Image(systemName: "chevron.left"),
                    onBack: closeView
                )
            }
        }
        .ndAlertOnWindow(isPresented: $showCustomPopup,
                         title: localizedString("Departure reminder"),
                         tips: localizedString("sure give up"),
                         message: localizedString("almost_auth_alert_message" ),
                         textAlignment: .leading,
                         backgroundImage: "common_alert_bg",
                         buttons: buttons)

    }
    
    private func closeView() {
        showCustomPopup = true
    }
}


